package com.zjdiepu.www.biz.other;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.base.DpBaseDataDictionaryMapper;
import com.zjdiepu.www.dao.other.OtherMessageHandleMapper;
import com.zjdiepu.www.ibiz.other.IOtherMessageHandleService;
import com.zjdiepu.www.model.base.dto.DpBaseDataDictionary;
import com.zjdiepu.www.model.base.ext.DpBaseDataDictionaryExt;
import com.zjdiepu.www.model.other.dto.OtherMessageHandle;
import com.zjdiepu.www.model.other.ext.OtherMessageHandleExt;

/**
 * Service层操作实现<br/>
 * @author zhangpp<br/>
 * @time 2017年06月01日 18时07分<br/>
 * @since 1.0<br/>
 */
@Service
public class OtherMessageHandleServiceImpl extends BaseService implements IOtherMessageHandleService {
	public static final Logger logger = LoggerFactory.getLogger(OtherMessageHandleServiceImpl.class);

	@Autowired
	private OtherMessageHandleMapper otherMessageHandleMapper;
	@Autowired
	private DpBaseDataDictionaryMapper dpBaseDataDictionaryMapper;
	

	@Override
	public void modifyAll(AbstractSysUser currentUser,
			OtherMessageHandle otherMessageHandle) {
		String typeEvent=otherMessageHandle.getTypeMsgEvent();
		OtherMessageHandle otherMessageHandle1=new OtherMessageHandle();
		otherMessageHandle1.setTypeMsgEvent(typeEvent);
		otherMessageHandleMapper.deletedRealRefined(otherMessageHandle1);
		saveAll(currentUser,otherMessageHandle);
//		String statusMsgHandle=otherMessageHandle.getStatusMsgHandle().substring(0,otherMessageHandle.getStatusMsgHandle().length()-1);
//		if(statusMsgHandle!=null && statusMsgHandle.contains(",")){
//    		String statusMsgHandles[]=statusMsgHandle.split(",");
//    		for (String msg : statusMsgHandles) {
//    			otherMessageHandle.initUpdateData(otherMessageHandle.getModifier(), otherMessageHandle.getModifierName());
//    			otherMessageHandle.setStatusMsgHandle(msg);
//    			otherMessageHandle.setDpStatus(1);
//    			// 影响的记录数
//    			int updateCount = otherMessageHandleMapper.updateByPrimaryKeySelective(otherMessageHandle);
//    			if(updateCount == 0){
//    				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
//    			} else if(updateCount > 1){
//    				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
//    			}
//			}
//    	}
		
		
		
	}
	
	@Override
	public void saveAll(AbstractSysUser currentUser,
			OtherMessageHandle otherMessageHandle) {
		if(otherMessageHandle.getStatusMsgHandle().contains(",")){
			String statusMsgHandle=otherMessageHandle.getStatusMsgHandle().substring(0,otherMessageHandle.getStatusMsgHandle().length()-1);
			DpBaseDataDictionaryExt dpBaseDataDictionaryExt=new DpBaseDataDictionaryExt();
			dpBaseDataDictionaryExt.setParentValue("MessageHandleStatus");
			dpBaseDataDictionaryExt.setDpStatus(1);
			List<DpBaseDataDictionary> list=dpBaseDataDictionaryMapper.selectByExt(dpBaseDataDictionaryExt);
			boolean flag=false;
			if(list !=null && list.size()>0){
				if(statusMsgHandle!=null && statusMsgHandle.contains(",")){
					String statusMsgHandles[]=statusMsgHandle.split(",");
					for(int i=0;i<list.size();i++){
						flag=false;
						for (String msg : statusMsgHandles) {
							if(list.get(i).getValue()!=null && list.get(i).getValue().equals(msg)){
								flag=true;
							}
						}
						otherMessageHandle.initInsertData(otherMessageHandle.getModifier(), otherMessageHandle.getModifierName());
						if(flag==true){
							otherMessageHandle.setValueMsgHandle(1);
						}else{
							otherMessageHandle.setValueMsgHandle(0);
						}
						otherMessageHandle.setStatusMsgHandle(list.get(i).getValue());
						otherMessageHandleMapper.insert(otherMessageHandle);
					}
				}else{
					for(int i=0;i<list.size();i++){
						if(list.get(i).getValue()!=null && list.get(i).getValue().equals(statusMsgHandle)){
							otherMessageHandle.setValueMsgHandle(1);
						}else{
							otherMessageHandle.setValueMsgHandle(0);
						}
						otherMessageHandle.setStatusMsgHandle(list.get(i).getValue());
						otherMessageHandle.initInsertData(otherMessageHandle.getModifier(), otherMessageHandle.getModifierName());
						otherMessageHandleMapper.insert(otherMessageHandle);
					}
				}
			}
		}
		
	}
	
	@Override
	public void saveUpdateAndDelete(OtherMessageHandle otherMessageHandle, OperateType operType) {
		switch (operType) {
		case add:
			otherMessageHandle.initInsertData(otherMessageHandle.getModifier(), otherMessageHandle.getModifierName());
			otherMessageHandleMapper.insert(otherMessageHandle);
			break;
		case edit:
			otherMessageHandle.initUpdateData(otherMessageHandle.getModifier(), otherMessageHandle.getModifierName());
			// 影响的记录数
			int updateCount = otherMessageHandleMapper.updateByPrimaryKeySelective(otherMessageHandle);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			otherMessageHandle.initDeleteData(otherMessageHandle.getModifier(), otherMessageHandle.getModifierName());
			// 影响的记录数
			int deleteCount = otherMessageHandleMapper.updateByPrimaryKeySelective(otherMessageHandle);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<OtherMessageHandle> selectByExt(OtherMessageHandleExt otherMessageHandleExt) {
		return otherMessageHandleMapper.selectByExt(otherMessageHandleExt);
	}
	
	@Override
	public int countByExt(OtherMessageHandleExt otherMessageHandleExt){
		return otherMessageHandleMapper.countByExt(otherMessageHandleExt);
	}
	
	@Override
	public OtherMessageHandle selectById(String id) {
		return otherMessageHandleMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			otherMessageHandleMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(OtherMessageHandle otherMessageHandle) {
		return otherMessageHandleMapper.countRefined(otherMessageHandle);
	}

	@Override
	public OtherMessageHandle selectRefined(OtherMessageHandle otherMessageHandle) {
		return otherMessageHandleMapper.selectRefined(otherMessageHandle);
	}
	
	@Override
	public int deletedRefined(OtherMessageHandle otherMessageHandle) {
		return otherMessageHandleMapper.deletedRefined(otherMessageHandle);
	}

	@Override
	public boolean repeat(OtherMessageHandle otherMessageHandle) {
		int count = countRefined(otherMessageHandle);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(otherMessageHandle.getId())) {
			return true;
		}
		OtherMessageHandle dbItem = selectRefined(otherMessageHandle);
		if(null == dbItem) {
			return false;
		}
		return !otherMessageHandle.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return otherMessageHandleMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<OtherMessageHandle> otherMessageHandles) {
		return otherMessageHandleMapper.insertBatch(otherMessageHandles);
	}

	@Override
	public int countMulitByExt(OtherMessageHandleExt otherMessageHandleExt) {
		
		return otherMessageHandleMapper.countMulitByExt(otherMessageHandleExt);
	}

	@Override
	public List<OtherMessageHandle> selectMulitByExt(
			OtherMessageHandleExt otherMessageHandleExt) {
		return otherMessageHandleMapper.selectMulitByExt(otherMessageHandleExt);
	}


}
